﻿using System;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;

namespace CurrencySpider.BackGroundJob
{
    /// <summary>
    /// 触发器管理类
    /// </summary>
    public class QuartzManager
    {
        private static ISchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
        private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
        private static String TRIGGER_NAME = "EXTJWEB_TRIGGER_NAME";
        private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";

        /// <summary>
        /// 添加一个定时任务，使用默认的任务组名，触发器名，触发器组名
        /// </summary>
        /// <param name="jobName">任务名</param>
        /// <param name="time">时间设置，参考quartz说明文档 </param>
        /// <param name="type">任务类型（如：typeof(HelloJob3)） </param>
        public static void AddJob(String jobName, String time, Type type)
        {
            try
            {
                IScheduler sched = gSchedulerFactory.GetScheduler();
                IJobDetail jobDetail = new JobDetailImpl(jobName, JOB_GROUP_NAME, type); // 任务名，任务组，任务执行类  
                // 触发器  
                ITrigger trigger = new CronTriggerImpl(TRIGGER_NAME, TRIGGER_GROUP_NAME, jobName, JOB_GROUP_NAME, time);
                sched.ScheduleJob(jobDetail, trigger);
                // 启动  
                if (!sched.IsShutdown)
                {
                    sched.Start();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        /// <summary>
        /// 添加一个定时任务 
        /// </summary>
        /// <param name="jobName">任务名</param>
        /// <param name="jobGroupName">任务组名</param>
        /// <param name="triggerName">触发器名</param>
        /// <param name="triggerGroupName">触发器组名</param>
        /// <param name="time">时间设置，参考quartz说明文档</param>
        /// <param name="type">任务类型（如：typeof(HelloJob3)） </param>
        public static void AddJob(String jobName, String jobGroupName,
            String triggerName, String triggerGroupName,
            String time, Type type)
        {
            try
            {
                IScheduler sched = gSchedulerFactory.GetScheduler();
                IJobDetail jobDetail = new JobDetailImpl(jobName, jobGroupName, type); // 任务名，任务组，任务执行类  
                // 触发器  
                ITrigger trigger = new CronTriggerImpl(triggerName, triggerGroupName, jobName, jobGroupName, time);
                sched.ScheduleJob(jobDetail, trigger);
                // 启动  
                if (!sched.IsShutdown)
                {
                    sched.Start();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 修改一个任务的触发时间(使用默认的任务组名，触发器名，触发器组名)
        /// </summary>
        /// <param name="jobName">任务名</param>
        /// <param name="time">时间设置，参考quartz说明文档</param>
        public static void ModifyJobTime(String jobName, String time, Type type)
        {
            try
            {
                IScheduler sched = gSchedulerFactory.GetScheduler();
                var trigger = sched.GetTrigger(new TriggerKey(TRIGGER_NAME, TRIGGER_GROUP_NAME));
                if (trigger == null)
                {
                    return;
                }
                RemoveJob(jobName);
                AddJob(jobName, time, type);
            }
            catch (Exception e)
            {

                throw e;
            }
        }

        /// <summary>
        /// 移除一个任务(指定的任务名、任务组、触发器名、触发器组、时间设置，参考quartz说明文档)
        /// </summary>
        /// <param name="jobName">任务名</param>
        /// <param name="jobGroup">任务组</param>
        /// <param name="triggerName">触发器名</param>
        /// <param name="triggerGroupName">触发器组</param>
        /// <param name="time">时间设置，参考quartz说明文档</param>
        public static void ModifyJobTime(string jobName, string jobGroup, String triggerName,
                String triggerGroupName, String time, Type type)
        {
            try
            {
                IScheduler sched = gSchedulerFactory.GetScheduler();
                var trigger = sched.GetTrigger(new TriggerKey(triggerName, triggerGroupName));
                if (trigger == null)
                {
                    return;
                }
                RemoveJob(jobName, jobGroup, triggerName, triggerGroupName);
                AddJob(jobName, time, triggerName, triggerGroupName, time, type);
            }
            catch (Exception e)
            {

                throw e;
            }
        }




        /// <summary>
        /// 移除一个任务 
        /// </summary>
        /// <param name="jobName">任务名</param>
        /// <param name="jobGroupName">任务组</param>
        /// <param name="triggerName">触发器名</param>
        /// <param name="triggerGroupName">触发器组</param>
        public static void RemoveJob(String jobName, String jobGroupName,
            String triggerName, String triggerGroupName)
        {
            try
            {
                var sched = gSchedulerFactory.GetScheduler();
                sched.PauseTrigger(new TriggerKey(triggerName, triggerGroupName)); // 停止触发器  
                sched.UnscheduleJob(new TriggerKey(triggerName, triggerGroupName)); // 移除触发器  
                sched.DeleteJob(new JobKey(jobName, jobGroupName)); // 删除任务  
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        /// <summary>
        /// 移除一个任务 
        /// </summary>
        /// <param name="jobName">任务名</param>
        public static void RemoveJob(String jobName)
        {
            try
            {
                var sched = gSchedulerFactory.GetScheduler();
                sched.PauseTrigger(new TriggerKey(jobName, TRIGGER_GROUP_NAME));// 停止触发器  
                sched.UnscheduleJob(new TriggerKey(jobName, TRIGGER_GROUP_NAME));// 移除触发器  
                sched.DeleteJob(new JobKey(jobName, JOB_GROUP_NAME));// 删除任务  
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 启动所有定时任务
        /// </summary>
        public static void StartJobs()
        {
            try
            {
                var sched = gSchedulerFactory.GetScheduler();
                sched.Start();
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        /// <summary>
        /// 关闭所有定时任务
        /// </summary>
        public static void ShutdownJobs()
        {
            try
            {
                var sched = gSchedulerFactory.GetScheduler();
                if (!sched.IsShutdown)
                {
                    sched.Shutdown();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}